package com.tencent.bk.sdk.iam.constants;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.tencent.bk.sdk.iam.dto.expression.ExpressionDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public enum ExpressionOperationEnum {
    NOT("NOT", Collections.singletonList(ExpressionDTO.class)),
    AND("AND", Collections.singletonList(ExpressionDTO.class)),
    OR("OR", Collections.singletonList(ExpressionDTO.class)),
    EQUAL("eq", Arrays.asList(String.class, Number.class, Boolean.class)),
    NOT_EQUAL("not_eq", Arrays.asList(String.class, Number.class, Boolean.class)),
    IN("in", Arrays.asList(String.class, Number.class, Boolean.class)),
    NOT_IN("not_in", Arrays.asList(String.class, Number.class, Boolean.class)),
    CONTAIN("contains", Collections.singletonList(String.class)),
    NOT_CONTAIN("not_contains", Collections.singletonList(String.class)),
    START_WITH("starts_with", Collections.singletonList(String.class)),
    NOT_START_WITH("not_starts_with", Collections.singletonList(String.class)),
    END_WITH("ends_with", Collections.singletonList(String.class)),
    NOT_END_WITH("not_ends_with", Collections.singletonList(String.class)),
    LESS_THEN("lt", Collections.singletonList(Number.class)),
    LESS_THAN_OR_EQUAL("lte", Collections.singletonList(Number.class)),
    GREATER_THAN("gt", Collections.singletonList(Number.class)),
    GREATER_THAN_OR_EQUAL("gte", Collections.singletonList(Number.class)),
    ANY("any", Collections.singletonList(String.class));

    @JsonValue
    private String operator;
    private List<Class<?>> supportClass;

    @JsonCreator
    public static ExpressionOperationEnum parseOperator(String operatorStr) {
        if (StringUtils.isBlank(operatorStr)) {
            return null;
        } else {
            for (ExpressionOperationEnum operator:values()) {
                if (operator.getOperator().equals(operatorStr)) {
                    return operator;
                }
            }
            return null;
        }
    }

    public String getOperator() {
        return this.operator;
    }

    public List<Class<?>> getSupportClass() {
        return this.supportClass;
    }

    private ExpressionOperationEnum(String operator, List supportClass) {
        this.operator = operator;
        this.supportClass = supportClass;
    }
}
